package cn.lijiajia3515.cairo.system.modules.area;

import cn.lijiajia3515.cairo.system.modules.ExtensionConstants;

import java.util.Map;
import java.util.Optional;

public class AreaConverter {
	public static AreaTree areaTree(AreaMongo source) {
		return AreaTree.builder().id(source.getId()).parent(source.getParent()).name(source.getName()).build();
	}

	public static Area area(AreaMongo m, Map<String, String> extensionMap) {
		AreaExtension extension = Optional.ofNullable(extensionMap.get(ExtensionConstants.AREA)).map(AreaExtension::valueOf).orElse(AreaExtension.ALL);
		Area.AreaBuilder<?, ?> builder = Area.builder();
		if (extension.fields().contains(AreaField.ID)) builder.id(m.getId());
		if (extension.fields().contains(AreaField.NAME)) builder.name(m.getName());
		if (extension.fields().contains(AreaField.PARENT)) builder.parent(m.getParent());
		if (extension.fields().contains(AreaField.LEVEL)) builder.level(m.getLevel());
		if (extension.fields().contains(AreaField.IDS)) builder.ids(m.getIds());
		if (extension.fields().contains(AreaField.NAMES)) builder.names(m.getNames());

		return builder.build();
	}
}
